﻿using Tetris.Game.Common.TetrisCenter.TetrisBase;
using Tetris.Game.ControlsBase;
using Tetris.Game.ControlsBase.Common;

namespace Tetris.Game.Common.Algorithm
{
    public class GeneticAlgorithm : IAlgorithm
    {
        private readonly int _populationSize = 50;
        private readonly Random _random = new Random();

        public Func<ShapeType, bool, BlockShapeBase?>? CreationBlockShapeCallback { get; set; }

        public GeneticAlgorithm()
        {

        }
        public ChooseBestMoveResult ChooseBestMove(BlockShapeBase piece, BlockControl[,] grid)
        {
            throw new NotImplementedException();
        }
        private List<Chromosome> InitializePopulation(int geneLength)
        {
            List<Chromosome> population = new List<Chromosome>();
            for (int i = 0; i < _populationSize; i++)
            {
                Chromosome chromosome = new Chromosome();
                for (int j = 0; j < geneLength; j++)
                {
                    chromosome.Genes.Add((DirectionType)new Random().Next(Enum.GetNames(typeof(DirectionType)).Length));
                }
                population.Add(chromosome);
            }
            return population;
        }
        /// <summary>
        /// 实现选择函数，选择最适合的染色体进行繁殖
        /// </summary>
        /// <param name="population"></param>
        /// <returns></returns>
        private Chromosome Select(List<Chromosome> population)
        {
            // 实现轮盘赌选择方法
            double totalFitness = population.Sum(c => c.Fitness);
            double randomPoint = new Random().NextDouble() * totalFitness;
            double currentSum = 0;
            foreach (var chromosome in population)
            {
                currentSum += chromosome.Fitness;
                if (currentSum >= randomPoint)
                {
                    return chromosome;
                }
            }
            return null;
        }
        /// <summary>
        /// 实现交叉函数，通过交换两个染色体的部分基因来产生新的染色体
        /// </summary>
        /// <param name="parent1"></param>
        /// <param name="parent2"></param>
        /// <returns></returns>
        private Chromosome Crossover(Chromosome parent1, Chromosome parent2)
        {
            Chromosome child = new Chromosome();
            int crossPoint = new Random().Next(parent1.Genes.Count);
            child.Genes.AddRange(parent1.Genes.Take(crossPoint));
            child.Genes.AddRange(parent2.Genes.Skip(crossPoint));
            return child;
        }
        /// <summary>
        /// 实现变异函数，通过随机改变染色体中的某些基因来增加遗传多样性
        /// </summary>
        /// <param name="chromosome"></param>
        /// <param name="mutationRate"></param>
        private void Mutate(Chromosome chromosome, double mutationRate)
        {
            Random rand = new Random();
            for (int i = 0; i < chromosome.Genes.Count; i++)
            {
                if (rand.NextDouble() < mutationRate)
                {
                    chromosome.Genes[i] = (DirectionType)rand.Next(Enum.GetNames(typeof(DirectionType)).Length);
                }
            }
        }
        /// <summary>
        /// 实现评估函数，通过某种方式（例如模拟游戏并计算消除的行数）来评估染色体的适应度
        /// </summary>
        /// <param name="chromosome"></param>
        /// <returns></returns>
        private double Evaluate(Chromosome chromosome)
        {
            // 模拟游戏并计算适应度
            return 0; // 返回计算得到的适应度分数
        }
    }
    /// <summary>
    /// 染色体类
    /// </summary>
    public class Chromosome
    {
        public List<DirectionType> Genes { get; set; }
        public double Fitness { get; set; }

        public Chromosome()
        {
            Genes = new List<DirectionType>(4);
            Fitness = 0;
        }
    }
}
